﻿@model IEnumerable<KendoUIGridSignalR.Models.StockData>
@{
    ViewBag.Title = "webapps-in-action.com";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@Html.Kendo().Grid(Model).Name("StockGrid").Columns(columns =>
        {
            columns.Bound(p => p.Name);
            columns.Bound(p => p.Price);
            columns.Bound(p => p.LastChange);
        }).DataSource(dataSource => dataSource
            .Ajax()
            .Read(read => read.Action("Read", "Home")))
<br />
<a href="#" onclick="updateStockNow('B')">Update B everywhere now</a>
<script type="text/javascript">

    // Author: Otto Neff | webapps-in-action.com

    $(function () {
        var stockHub = $.connection.stockHub;
        stockHub.client.updateStock = function (stock) {
            updateRow(stock);
        };
        $.connection.hub.start();
    });

    function updateRow(stock) {
        var data = $("#StockGrid").data("kendoGrid").dataSource.data();
        for (var i = 0; i < data.length; i++) {
            var dataItem = data[i];
            if (dataItem.Name == stock.Name) {
                dataItem.set("LastChange", stock.LastChange);
                dataItem.set("Price", stock.Price);
                $("#StockGrid").data("kendoGrid").tbody.find("tr[data-uid=" + dataItem.uid + "]").effect("highlight");
            }
        }
    }

    function updateStockNow(e) {
        $.connection.stockHub.server.getStock(e);
    }

</script>
